<?php
session_start();
class DB
	{
	var $spojeni;
	function DbConnect(){
			$this->spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" ); 
			if (!$this->spojeni) { 
				return false; 
			}
			mysql_query("SET CHARACTER SET utf8;");
			mysql_select_db("havelvoj");
			return true;
		}
	function Checklogin($login, $password){
if(!DbConnect){return false;}
else {

//echo 1; (OP debugging):D

$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");
		
		
		
		
$query = "SELECT Pass, Id, Name FROM U7Users WHERE Name='$login'";
//$query = "SELECT Date,Bmi FROM U6Bmi WHERE Date > '$SetDate'";
	    $rs = mysql_query($query);
		//echo 2;
	    if (!$rs) {
		echo mysql_error();
		    return false;
			}else{
			//echo 3;
			
			while ($current=mysql_fetch_array($rs)){
			if(verify($password,$current['Pass'])){
			$_SESSION['Name']=$login;
			$_SESSION['ID']=$current['Id'];	
			
			//echo 4;
			return true;
						}
			}
			return false;
						
			}	
}
}

function Register($login, $password, $email){
$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");
$query = "SELECT Name FROM U7Users WHERE Name = '$login'";
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 1. query: $query".mysql_error();
			return false;
		   }
		   

$num = mysql_fetch_assoc($rs);
if($num>0){return false;}

$query = "INSERT INTO U7Users (Name, Pass, email)
			VALUES ('".$login."','".generateHash($password)."','".$email."')";
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 1. query: $query".mysql_error();
			return false;
		   }
		   return true;

}

function GetData($ID){
$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");


$query = "SELECT BMI, Date, User_ID, Entry_ID FROM U7Entries WHERE User_ID = '$ID'";
			
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 1. query: $query".mysql_error();
			return false;
		   }
		   return $rs;

}

function GetDetail($ID){
$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");


$query = "SELECT BMI, Date, Weight, Height FROM U7Entries WHERE Entry_ID = '$ID'";
			
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 1. query: $query".mysql_error();
			return false;
		   }
		   return $rs;

}


function NewEntry($ID, $Height, $Weight){

$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");

$BMI=$Weight/($Height* $Height/10000);
$query = 
		"INSERT INTO U7Entries (User_ID, BMI, Date, Weight, Height)
			VALUES ('".$ID."','".$BMI."','".date('Y-m-d')."','".$Weight."','".$Height."')";	
$rs = mysql_query($query);
if (!$rs) {
		    echo "Chyba v 1. query: $query".mysql_error();
			return false;
		   }
		   return true;

}

function DeleteEntry($ID){
$spojeni= mysql_connect("localhost",  "havelvoj",  "shaus1be" );
	if (!$spojeni) { 
				die(' Pripojení k databázi neúspešné: ' . mysql_error()); 
	}
		mysql_query("SET CHARACTER SET utf8;");
		mysql_query("SET NAMES utf8");
		mysql_select_db("havelvoj");


$query = "DELETE FROM U7Entries WHERE Entry_ID = '$ID'";	
$rs = mysql_query($query);
if (!$rs) {
		    //echo "Chyba v 1. query: $query".mysql_error();
			return false;
		   }
		   return true;

}

	
}			
?>